{extend name="base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
    <div class="layui-colla-item">
        <h2 class="layui-colla-title">操作提示</h2>
        <ul class="layui-colla-content layui-show">
           <!-- <li>您可以通过选择所属广告位来获取该广告位下的所有广告</li>-->
        </ul>
    </div>
</div>

<div class="ns-single-filter-box">
    <button class="layui-btn ns-bg-color" onclick="add()">发布新BANNER</button>
</div>

<div class="ns-screen layui-collapse" lay-filter="selection_panel">
    <div class="layui-colla-item">
        <form class="layui-colla-content layui-form layui-show">
            <div class="layui-form-item">

                <div class="layui-inline">
                    <label class="layui-form-label">BANNER标题：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="search_text" name="search_text" placeholder="请输入BANNER标题" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">状态：</label>
                    <div class="layui-input-inline">
                        <select name="is_show" lay-filter="is_show">
                            <option value="">全部</option>
                            <option value="1">有效</option>
                            <option value="0">无效</option>
                        </select>
                    </div>
                </div>

            </div>

            <div class="ns-form-row">
                <button class="layui-btn ns-bg-color" lay-submit lay-filter="search_website">筛选</button>

                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </form>
    </div>
</div>
<!--<div class="ns-single-filter-box">
    <button class="layui-btn ns-bg-color" onclick="add()">添加轮播</button>

    <div class="layui-form">

        <div class="layui-input-inline">
            <select name="is_show">
                <option value="">状态</option>
                <option value="1">显示</option>
                <option value="0">隐藏</option>
            </select>
        </div>

        <div class="layui-input-inline ns-len-mid">
            <input type="text" name="search_text" placeholder="请输入轮播名称" class="layui-input">
            <button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
                <i class="layui-icon">&#xe615;</i>
            </button>
        </div>
    </div>


</div>-->

<!-- 列表 -->
<table id="banner_list" lay-filter="banner_list"></table>

<!-- 广告图片 -->
<script type="text/html" id="banner_image">
    <div class="ns-img-box">
        <img layer-src src="{{ns.img(d.image)}}" />
    </div>
</script>

<!-- 排序 -->
<script type="text/html" id="slide_sort">
    <input name="slide_sort" type="number" onchange="editSort({{d.banner_id}},'slide_sort', this)" value="{{d.slide_sort}}" class="layui-input ns-sort-len">
</script>

<!-- 广告链接 -->
<script type="text/html" id="banner_url">
	<a href="{{d.banner_url}}">{{d.banner_url}}</a>
</script>

<!-- 操作 -->
<script type="text/html" id="action">
    <div class="ns-table-btn">
        <a class="layui-btn" lay-event="edit">编辑</a>
        <a class="layui-btn" lay-event="delete">删除</a>
        {{#  if(d.is_show == 0){ }}
        <a class="layui-btn" lay-event="show">有效</a>
        {{#  } }}
        {{#  if(d.is_show == 1){ }}
        <a class="layui-btn" lay-event="hide">无效</a>
        {{#  } }}
    </div>
</script>

<!-- 批量删除 -->
<script type="text/html" id="toolbarOperation">
    <button class="layui-btn layui-btn-primary" lay-event="del">批量删除</button>
</script>
<!-- 批量删除 -->
<script type="text/html" id="batchOperation">
    <button class="layui-btn layui-btn-primary" lay-event="del">批量删除</button>
</script>
{/block}
{block name="script"}
<script>
    layui.use('form', function () {
        var table,
            form = layui.form,
            repeat_flag = false; //防重复标识
        form.render();

        table = new Table({
            elem: '#banner_list',
            url: ns.url("admin/banner/lists"),
            where: {

            },
            cols: [
                [/*{
                    width: '3%',
                    type: 'checkbox',
                    unresize: 'false',
                }, */{
                    title: 'BANNER图片',
                    unresize: 'false',
                    templet: '#banner_image',
                    width: '15%'
                },{
                    field: 'banner_title',
                    title: 'BANNER名称',
                    unresize: 'false',
                    width: '20%'
                }, {
                    field: 'type',
                    title: '位置',
                    unresize: 'false',
                    width: '10%',
                    templet: function(data){
                        if (data.type==0){
                            return '首页';
                        } else if(data.type==1){
                            return '商城首页';
                        } else if(data.type==2){
                            return '本地生活';
                        }
                    }
                }, /*{
                    title: '轮播链接',
                    unresize: 'false',
                    width: '25%',
					templet: function(data) {
                    	var banner_url = data.banner_url;
                    	if(banner_url){
                            banner_url = JSON.parse(data.banner_url);
							return banner_url.title ? banner_url.title : '';
                        }else{
                    		return '';
                        }
					}
                }, */ {
                    title: '状态',
                    unresize: 'false',
                    width: '10%',
                    templet: function(data) {
                        return data.is_show==1?'有效':'无效';
                    }
                },{
                    title: '排序',
                    unresize: 'false',
                    field: 'slide_sort',
                    //templet: '#slide_sort',
                    width: '10%'
                }, {
                    title: '发布人',
                    unresize: 'false',
                    width: '10%',
                    templet: function(data) {
                        return data.username+'/'+data.uid;
                    }
                },{
                    width: '15%',
                    title: '发布时间',
                    unresize : 'true',
                    templet: function (d) {
                        return ns.time_to_date(d.create_time);
                    }
                },   {
                    title: '操作',
                    toolbar: '#action',
                    unresize: 'false',
                    width: '10%'
                }]
            ],
            //bottomToolbar: "#batchOperation",
            //toolbar: "#toolbarOperation"
        });

        /**
         * 监听工具栏操作
         */
        table.tool(function(obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit': //编辑
                    location.href = ns.url("admin/banner/editbanner",{"banner_id":data.banner_id});
                    break;
                case 'delete': //删除
                    deleteBanner(data.banner_id);
                    break;
                case 'show': //显示
                    showBanner(data.banner_id);
                    break;
                case 'hide': //隐藏
                    hideBanner(data.banner_id);
                    break;
            }
        });

        /**
         * 批量操作
         */
        table.bottomToolbar(function(obj) {
            if (obj.data.length < 1) {
                layer.msg('请选择要操作的数据');
                return;
            }

            switch (obj.event) {
                case "del":
                    var id_array = new Array();
                    for (i in obj.data) id_array.push(obj.data[i].banner_id);
                    deleteBanner(id_array.toString());
                    break;
            }
        });

        /**
         * 批量操作
         */
        table.toolbar(function(obj) {
            if (obj.data.length < 1) {
                layer.msg('请选择要操作的数据');
                return;
            }

            switch (obj.event) {
                case "del":
                    var id_array = new Array();
                    for (i in obj.data) id_array.push(obj.data[i].banner_id);
                    deleteBanner(id_array.toString());
                    break;
            }
        });

        /**
         * 删除
         */
        function deleteBanner(banner_ids) {
            if (repeat_flag) return false;
            repeat_flag = true;

            layer.confirm('确定要删除该轮播吗?', function() {
                $.ajax({
                    url: ns.url("admin/banner/deleteBanner"),
                    data: {'banner_ids': banner_ids},
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;

                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            }, function () {
                layer.close();
                repeat_flag = false;
            });
        }

        function showBanner(banner_id) {
            if (repeat_flag) return false;
            repeat_flag = true;

            layer.confirm('确定要显示该轮播吗?', function() {
                $.ajax({
                    url: ns.url("admin/banner/editBannerStatus"),
                    data: {'banner_id': banner_id,'is_show':1},
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;

                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            }, function () {
                layer.close();
                repeat_flag = false;
            });
        }

        function hideBanner(banner_id) {
            if (repeat_flag) return false;
            repeat_flag = true;

            layer.confirm('确定要隐藏该轮播吗?', function() {
                $.ajax({
                    url: ns.url("admin/banner/editBannerStatus"),
                    data: {'banner_id': banner_id,'is_show':0},
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;

                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            }, function () {
                layer.close();
                repeat_flag = false;
            });
        }

        /**
         * 监听搜索
         */
        form.on('submit(search_website)', function(data){
            table.reload({
                page: {
                    curr: 1
                },
                where: data.field
            });
            return false;
        });

    });

    // 监听单元格编辑
    function editSort(banner_id, type, event){
        var value = $(event).val();

        if(!new RegExp("^-?[1-9]\\d*$").test(value)){
            layer.msg("排序号只能是整数");
            return ;
        }
        if(value<0){
			layer.msg("排序号必须大于0");
			return ;
        }

        $.ajax({
            type: 'POST',
            url: ns.url("admin/banner/editBannerField"),
            data: {'type': type, 'value': value, 'banner_id': banner_id},
            dataType: 'JSON',
            success: function(res) {
                layer.msg(res.message);
                if(res.code==0){
                    table.reload();
                }
            }
        });
    }

    function add() {
        location.href = ns.url("admin/banner/addBanner");
    }
</script>
{/block}